import { observable, action, toJS } from 'mobx';
import {listStore} from '.';
import _ from 'lodash';
import { getHeight } from '../../components/list/utils';

export interface VerticalListStoreProps {

}
export default class VerticalListStore implements VerticalListStoreProps {
    Search: listStore;
    constructor(search: listStore) {
        this.Search = search;
    }
    @observable current:number =1;
    @observable pageSize:number = 10; 
    @observable allDatas:Array<any> = [];
    @observable loading:boolean = false;
    @observable hasMore:boolean = true;   //是否有更多
    @observable rowCheckKey:Array<String> = [];
    @observable selectValue:string = '';

    @action
    newDataBacks=()=>{
        const currentDatas = toJS(this.Search.newTable.datas);
        this.allDatas = currentDatas;
        this.loading = false;
        if(currentDatas.length>0){getHeight( this.Search)}
    }

    @action
    dataBacks=()=>{
        const currentDatas = toJS(this.Search.newTable.datas);
        this.allDatas = this.allDatas.concat(currentDatas);
        this.loading = false;
    }

    @action
    loadMorePage = (current = this.current, pageSize = this.pageSize) => {
        const {sessionKey,newTable} = this.Search;
            this.current = current;
            newTable.getDatas(sessionKey,current,pageSize,{},this.dataBacks);
    }

    @action
    updateLikeOrDis=(billid,isshowlistspan=[],likesinfospan)=>{
        this.allDatas = this.allDatas.map(item=>{
            if(item.randomFieldId == billid){
                if(isshowlistspan.length > 0 ){
                    return {...item,likesinfospan,isshowlistspan:JSON.stringify(isshowlistspan)}
                }else{
                    return {...item,likesinfospan}
                }
            }
            return {...item}
        })
    }

    @action
    selectRowKeyAll = (isClear?) => {
        let selectStr = ''
        if(isClear){
            this.Search.selectRowkeys = [];
        }else{
            const rowkeysarr:Array<string> = [];
            _.forEach(this.allDatas,item=>{
                rowkeysarr.push(this.getRowKey(item));
            })
            this.Search.update({selectRowkeys:rowkeysarr})
            selectStr = _.uniq(rowkeysarr.map(d => d.split('_')[0])).join(',');
        }
        this.selectValue = selectStr;
    }

    getRowKey = (data) => {
        return data.randomFieldId ? (data.randomFieldSubKeyd_id ?
            data.randomFieldId + '_' + data.randomFieldSubKeyd_id : data.randomFieldId) : '';
    }

    @action
    update = (param) => {
        for (let key in param) {
            this[key] = param[key]
        }
    }

    @action
    clear=()=>{
        this.current = 1;
        this.allDatas = [];
        this.rowCheckKey = [];
        this.selectValue = '';
        this.hasMore = true;
    }

}
